/*
*	Copyright (C) 2014  Julio Montes, Mario Carrillo
*	
*	Julio Montes e-mail: imc.coder@gmail.com 
*
*	Mario Carrillo e-mail: mario_90ago@live.com.mx
*
*	BSD 3-Clause License
*/

#include "alliance/include/createprjdlg.h"
#include "alliance/include/path.h"
#include "alliance/include/projectmgr.h"

#include <wx/wx.h>

namespace alliance
{
    CreateProjectDialog::CreateProjectDialog( wxWindow *parent )
        : wxDialog( parent, wxID_ANY, wxT( "Create Project" ), wxDefaultPosition, wxSize( 400, 340 ) )
    {
        wxBoxSizer *l_bsMain = new wxBoxSizer( wxVERTICAL );

        wxBitmap image( Path::s_dirImages + wxT( "alliance_logo.png" ), wxBITMAP_TYPE_PNG );
        wxStaticBitmap *l_imageAlliance = new wxStaticBitmap( this, wxID_ANY, image );
        l_bsMain->Add( l_imageAlliance, 0, wxALIGN_CENTER_HORIZONTAL );

        l_bsMain->AddSpacer( 15 );

        wxStaticText *l_stProjectName = new wxStaticText( this, wxID_ANY, wxT( "Project Name:" ) );
        l_bsMain->Add( l_stProjectName, 0, wxLEFT, 20 );
        l_bsMain->AddSpacer( 5 );
        m_tcProjectName = new wxTextCtrl( this, wxID_ANY, wxT( "" ), wxDefaultPosition, wxDefaultSize, wxTE_PROCESS_ENTER );
        l_bsMain->Add( m_tcProjectName, 0, wxEXPAND | wxLEFT | wxRIGHT, 20 );
        m_tcProjectName->SetFocus();

        l_bsMain->AddSpacer( 15 );

        wxStaticText *l_stProjectDir = new wxStaticText( this, wxID_ANY, wxT( "Project Directory:" ) );
        l_bsMain->Add( l_stProjectDir, 0, wxLEFT, 20 );
        l_bsMain->AddSpacer( 5 );
        wxBoxSizer *l_bsProjectDir = new wxBoxSizer( wxHORIZONTAL );
        m_tcProjectDir = new wxTextCtrl( this, wxID_ANY, wxGetHomeDir() );
        m_tcProjectDir->SetEditable( false );
        wxButton *l_btnDir = new wxButton( this, wxID_ANY, wxT( "..." ), wxDefaultPosition, wxSize( 30, 30 ) );
        l_bsProjectDir->Add( m_tcProjectDir, 1, wxALIGN_CENTER_VERTICAL );
        l_bsProjectDir->Add( l_btnDir, 0, wxALIGN_CENTER_VERTICAL );
        l_bsMain->Add( l_bsProjectDir, 0, wxEXPAND | wxLEFT | wxRIGHT, 20 );

        l_bsMain->AddSpacer( 15 );

        wxStaticText *l_stProjectFullPath = new wxStaticText( this, wxID_ANY, wxT( "Project full path:" ) );
        l_bsMain->Add( l_stProjectFullPath, 0, wxLEFT, 20 );
        l_bsMain->AddSpacer( 5 );
        m_tcProjectFullPath = new wxTextCtrl( this, wxID_ANY, wxT( "" ), wxDefaultPosition, wxDefaultSize, wxTE_PROCESS_ENTER );
        m_tcProjectFullPath->SetEditable( false );
        l_bsMain->Add( m_tcProjectFullPath, 0, wxEXPAND | wxLEFT | wxRIGHT, 20 );

        l_bsMain->AddSpacer( 25 );

        wxBoxSizer *l_bsButtons = new wxBoxSizer( wxHORIZONTAL );
        wxButton *l_btnOk = new wxButton( this, wxID_OK );
        l_bsButtons->Add( l_btnOk, 0 );
        l_bsButtons->AddSpacer( 10 );
        wxButton *l_btnCancel = new wxButton( this, wxID_CANCEL );
        l_bsButtons->Add( l_btnCancel, 0 );
        l_bsMain->Add( l_bsButtons, 0, wxALIGN_RIGHT | wxRIGHT, 20 );

        SetSizer( l_bsMain );

        m_tcProjectName->Connect( wxID_ANY, wxEVT_COMMAND_TEXT_ENTER, wxCommandEventHandler( CreateProjectDialog::OnOk ), NULL, this );
        m_tcProjectName->Connect( wxID_ANY, wxEVT_COMMAND_TEXT_UPDATED, wxCommandEventHandler( CreateProjectDialog::OnTextUpdate ), NULL, this );
        l_btnDir->Connect( wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( CreateProjectDialog::OnSearchDirectory ), NULL, this );
        l_btnOk->Connect( wxID_OK, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( CreateProjectDialog::OnOk ), NULL, this );
    }

    CreateProjectDialog::~CreateProjectDialog()
    {

    }

    wxString CreateProjectDialog::GetProjectFilename() const
    {
        return m_tcProjectFullPath->GetValue();
    }

    wxString CreateProjectDialog::GetProjectName() const
    {
        return m_tcProjectName->GetValue();
    }

    void CreateProjectDialog::UpdateFullPath()
    {
        const wxString &projectName = m_tcProjectName->GetValue();

        if( !projectName.IsEmpty() )
        {
            wxString fullPath = m_tcProjectDir->GetValue();
            if( !fullPath.EndsWith( _T( "/" ) ) )
            {
                fullPath += _T( "/" );
            }

            fullPath += projectName + _T( "/" ) + projectName + _T( "." ) + ProjectManager::s_projectExtension;

            m_tcProjectFullPath->SetValue( fullPath );
        }
        else
        {
            m_tcProjectFullPath->SetValue( _T( "" ) );
        }
    }

    void CreateProjectDialog::OnOk( wxCommandEvent &evt )
    {
        if( !m_tcProjectFullPath->GetValue().IsEmpty() )
        {
            /*if( wxFileExists( m_tcProjectFullPath->GetValue() ) )
            {
                const int result = wxMessageBox( wxT( "The file " ) + m_tcProjectFullPath->GetValue() +
                    wxT( " already exists, do you want replace this file?" ),
                    wxT( "Replace file" ), wxYES | wxNO | wxCANCEL );
                if( result != wxYES )
                {
                    return;
                }
            }*/

            EndModal( wxID_OK );
        }
        else
        {
            //TODO: show error, the project not has name
        }
    }

    void CreateProjectDialog::OnSearchDirectory( wxCommandEvent &evt )
    {
        wxDirDialog dlg( this, wxT( "Select the folder" ), m_tcProjectDir->GetValue() );
        const int result = dlg.ShowModal();
        if( result != wxID_CANCEL )
        {
            m_tcProjectDir->SetValue( dlg.GetPath() );
            UpdateFullPath();
        }
    }

    void CreateProjectDialog::OnTextUpdate( wxCommandEvent &evt )
    {
        UpdateFullPath();
    }
}
